package com.mezz.bones.im.logic.process.impl;

import com.mezz.bones.im.common.enums.ActionCodeEnum;
import com.mezz.bones.im.common.event.MessageSendWithUserIdEvent;
import com.mezz.bones.im.common.message.base.MessageHeader;
import com.mezz.bones.im.common.message.body.in.MessageHasBeenReadReq;
import com.mezz.bones.im.common.message.body.out.MessageHasBeenReadRes;
import com.mezz.bones.im.common.domain.user.ImUserConnectInfo;
import com.mezz.bones.im.common.process.IMessageProcessor;
import com.mezz.bones.im.logic.process.interfaces.AbstractMessageLogicProcess;
import com.mezz.bones.im.logic.service.IChatConversationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: zhuang
 * @Date: 2024/11/29 18:39
 * @Description: TODO
 * @Version: 1.0
 **/

@Service
public class MessageHasBeenReadProcess extends AbstractMessageLogicProcess<MessageHasBeenReadReq, Object> {

    @Resource
    private IMessageProcessor messagePublisher;

    @Resource
    private IChatConversationService chatConversationService;

    @Override
    public void preHandlerParam(ImUserConnectInfo senderUserConnectInfo, MessageHeader header, MessageHasBeenReadReq req) {
        req.setUserId(senderUserConnectInfo.getUserId());
    }

    @Override
    public String tag() {
        return ActionCodeEnum.CLIENT_MESSAGE_HAS_BEAN_READ.getCode();
    }


    private void sendMessage2Peer(MessageHeader header, MessageHasBeenReadRes res) {
        messagePublisher.sendMessage2UserId(new MessageSendWithUserIdEvent(header.getTraceId(), header.getActionCode(), res, res.getUserId()));
    }

    @Override
    public Object process(ImUserConnectInfo senderUserConnectInfo, MessageHeader header, MessageHasBeenReadReq req, Map<String, Object> ctx) {
        MessageHasBeenReadRes res = chatConversationService.messageHasBeenRead(req);

        //推送消息到对端会话
        sendMessage2Peer(header, res);
        return null;
    }
}
